<?php

include_once("../service/imageService.php");
include_once("../service/dishService.php");
include_once("../util/characterUtils.php");

$cateId = $_POST['foodType'];
$dishName = $_POST['dishName'];
$dishPrice = $_POST['dishPrice'];
$dishDescription = $_POST['dishDescription'];

$result = getAll();
$flag = true;
while ($cat = ConnectionManager::getInstance()->fetchArray()) {
    if (removeSignOfString($cat["DISH_NAME"]) == removeSignOfString($dishName)) {
        $flag = false;
        break;
    }
}
if ($flag) {
    addDish($cateId, $dishName, $dishPrice, trim($dishDescription));
    $currentDishID = getCurrentDishID();

    $img1 = $_FILES['img1'];
    $img2 = $_FILES['img2'];
    $img3 = $_FILES['img3'];

    $imageName1 = uploadImage($img1, '1', '', $currentDishID);
    resize($imageName1, $currentDishID);
    $imageName2 = uploadImage($img2, '2', '', $currentDishID);
    resize($imageName2, $currentDishID);
    $imageName3 = uploadImage($img3, '3', '', $currentDishID);
    resize($imageName3, $currentDishID);

    Header("Location: ../admin-page/addDish.php?message=ok&cateId=$cateId");
    return;
} else {
    Header("Location: ../admin-page/addDish.php?message=unique");
    return;
}

?>
